﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace RLGDashboard
{
    class PostMortemDriver
    {
        private StreamReader fileHandle;

        private EgtSensor tempSensor;
        private PressureSensor psiSensor;
        private AFRSensor afrSensor;
        private VoltageSensor voltageSensor;
        private RpmSensor rpmSensor;

        public List<Sensor> temperatureList = new List<Sensor>();
        public List<Sensor> pressureList = new List<Sensor>();
        public List<Sensor> afrList = new List<Sensor>();
        public List<Sensor> rpmList = new List<Sensor>();
        public List<Sensor> voltageList = new List<Sensor>();

        public PostMortemDriver(string fileName)
        {
            tempSensor = new EgtSensor();
            psiSensor = new PressureSensor();
            afrSensor = new AFRSensor();
            voltageSensor = new VoltageSensor();
            rpmSensor = new RpmSensor();

            fileHandle = new StreamReader(System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + @"\" + fileName);
        }

        public void parseFile()
        {
            while (!fileHandle.EndOfStream)
            {
                DateTime start = DateTime.Now;
                BluetoothTransmission transmission = new BluetoothTransmission();

                readNewTransmission(transmission);
                processTransmission(transmission);
            }
        }

        private void processTransmission(BluetoothTransmission transmission)
        {
            switch (transmission.messageID)
            {
                case 0: createNewNode(tempSensor, transmission, temperatureList); break;
                case 1: createNewNode(psiSensor, transmission, pressureList); break;
                case 2: createNewNode(rpmSensor, transmission, rpmList); break;
                case 3: rpmSensor.updateValues(transmission); break;
                case 4: createNewNode(voltageSensor, transmission, voltageList); break;
                case 5: createNewNode(afrSensor, transmission, afrList); break;
            }
        }

        private void createNewNode(Sensor sensor, BluetoothTransmission transmission, List<Sensor> List)
        {
            Sensor node = new Sensor();
            sensor.updateValues(transmission);

            node.currentSensorValues = sensor.currentSensorValues;
            node.timeStamp = sensor.timeStamp;
            node.timestampMinutes = sensor.timestampMinutes;

            List.Add(node);
        }

        private void readNewTransmission(BluetoothTransmission transmission)
        {
            string value;
            do
            {
                value = fileHandle.ReadLine();
            } while (value != "NEW");

            transmission.timestamp = UInt32.Parse(fileHandle.ReadLine());
            transmission.messageID = byte.Parse(fileHandle.ReadLine());
            transmission.sensors[0] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[1] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[2] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[3] = UInt16.Parse(fileHandle.ReadLine());

            //read off un-needed "END"
            fileHandle.ReadLine();
        }
    }
}
